/**
 * Copyright (c) 2011 The MITRE Corporation. All rights reserved.
 *
 * NOTICE
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-11-C-0001, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 */
package org.mitre.edxl.lib;

import java.util.ArrayList;
import java.util.List;

/**
 * Container for Target Area data.
 *
 * @author Danny Gagne
 * @author Rob Gustafson
 * @author Brian Wilkins
 * @author Greg Kern
 */
public class TargetArea {

  /**
   * Enum for the possible Target Area types.
   */
  public enum TargetAreaEnum {
    circle, polygon, country, subdivision, locCodeUN
  };

  private List<String> circleList;
  private List<String> polygonList;
  private List<String> countryList;
  private List<String> subdivisionList;
  private List<String> locCodeUNList;

  /**
   * Returns a XML representation of the TargetArea object.
   *
   * @return A String of the XML representation of the TargetArea object.
   */
  public String toXML() {
    StringBuilder sb =  new StringBuilder();

    sb.append(addStringListElement(this.circleList, "circle"));
    sb.append(addStringListElement(this.polygonList, "polygon"));
    sb.append(addStringListElement(this.countryList, "country"));
    sb.append(addStringListElement(this.subdivisionList, "subdivision"));
    sb.append(addStringListElement(this.locCodeUNList, "locCodeUN"));

    return sb.toString();
  }

  /**
   * Adds an Area to the appropriate Target Area list based on the
   * provided Target Area Type.
   *
   * @param targetAreaEnum The Target Area Type.
   * @param val The new Area to be added to the list.
   */
  public void addArea(TargetAreaEnum targetAreaEnum, String val) {
    switch (targetAreaEnum) {
    case circle:
      if (this.circleList == null) {
        this.circleList = new ArrayList<String>();
      }
      this.circleList.add(val);
      break;
    case polygon:
      if (this.polygonList == null) {
        this.polygonList = new ArrayList<String>();
      }
      this.polygonList.add(val);
      break;
    case country:
      if (this.countryList == null) {
        this.countryList = new ArrayList<String>();
      }
      this.countryList.add(val);
      break;
    case subdivision:
      if (this.subdivisionList == null) {
        this.subdivisionList = new ArrayList<String>();
      }
      this.subdivisionList.add(val);
      break;
    default:
      if (this.locCodeUNList == null) {
        this.locCodeUNList = new ArrayList<String>();
      }
      this.locCodeUNList.add(val);
      break;
    }
  }

  /**
   * Creates a String of XML elements with the given name, one for each
   * value in the list.
   *
   * @param valList The list of values to add XML element for.
   * @param elementName The name that should be given to the XML element.
   * @return The String containing the a series of XML elements.
   */
  private String addStringListElement(List<String> valList, String elementName) {
    String temp = "";

    if (valList != null) {
      for (String val : valList) {
        temp += "<" + elementName + ">" + val + "</" + elementName + ">";
      }
    }

    return temp;
  }

  /**
   * Get the appropriate Target Area list of the Area values based on the
   * provided Target Area Type.
   *
   * @param targetAreaEnum The Target Area Type.
   * @return The list of the Area values.
   */
  public List<String> getArea(TargetAreaEnum targetAreaEnum) {
    switch (targetAreaEnum) {
    case circle:
      return this.circleList;
    case polygon:
      return this.polygonList;
    case country:
      return this.countryList;
    case subdivision:
      return this.subdivisionList;
    default:
      return this.locCodeUNList;
    }
  }

  /**
   * Set the appropriate Target Area list of Area values based on the
   * provided Target Area Type.
   *
   * @param targetAreaEnum The Target Area Type.
   * @param targetAreaList The list to set the Area values to.
   */
  public void setArea(TargetAreaEnum targetAreaEnum, List<String> targetAreaList) {
    switch (targetAreaEnum) {
    case circle:
      this.circleList = targetAreaList;
      break;
    case polygon:
      this.polygonList = targetAreaList;
      break;
    case country:
      this.countryList = targetAreaList;
      break;
    case subdivision:
      this.subdivisionList = targetAreaList;
      break;
    default:
      this.locCodeUNList = targetAreaList;
      break;
    }
  }
}
